package com.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * dubbo config
 */
@SuppressWarnings("Duplicates")
@Configuration
public class DubboConfig {

    @Autowired
    private Environment env;

    /**
     * dubbo 内部扫描注解
     *
     * @return
     */
    @Bean
    public static AnnotationBean annotationBean() {
        AnnotationBean annotationBean = new AnnotationBean();
        annotationBean.setPackage("com");
        return annotationBean;
    }

    /**
     * 应用配置，用于配置当前应用信息，不管该应用是提供者还是消费者。
     *
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(env.getProperty("dubbo.application", "mobile_im"));
        applicationConfig.setOwner(env.getProperty("dubbo.owner"));
        applicationConfig.setRegistry(registryConfig());
        applicationConfig.setMonitor(monitorConfig());
        return applicationConfig;
    }

    /**
     * 监控中心配置，用于配置连接监控中心相关信息，可选。
     *
     * @return
     */
    @Bean
    public com.alibaba.dubbo.config.MonitorConfig monitorConfig() {
        return new com.alibaba.dubbo.config.MonitorConfig();
    }

    /**
     * 注册中心配置，用于配置连接注册中心相关信息。
     * 详细参考 ：http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%3Cdubbo%3Aservice%2F%3E
     */
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(env.getProperty("dubbo.registry.address"));
        registryConfig.setFile(env.getProperty("dubbo.registry.temp"));//动态注册中心列表存储文件
        registryConfig.setTimeout(Integer.valueOf(env.getProperty("dubbo.registry.timeout")));
        return registryConfig;
    }

    /***
     * 协议配置，用于配置提供服务的协议信息，协议由提供方指定，消费方被动接受。
     *
     * 详细参考 http://dubbo.io/Protocol+Config.htm
     *
     * @return
     */
    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig("dubbo");
        protocolConfig.setPort(Integer.parseInt(env.getProperty("dubbo.protocol.dubbo.port", "20880")));
        return protocolConfig;
    }

    /**
     * 详细参考
     * http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%3Cdubbo%3Aservice%2F%3E
     * @return
     */
    @Bean
    public ServiceConfig serviceConfig(){
        ServiceConfig serviceConfig=new ServiceConfig();
        serviceConfig.setRegistry(registryConfig());
        serviceConfig.setMonitor(monitorConfig());
        serviceConfig.setConnections(10000);
        serviceConfig.setOwner("chenlei");
        return serviceConfig;
    }

}
